/**
 * @Copyright 
 * @Project inetdisk
 * @CreateTime 2015-10-12 上午10:52:35
 */
package ren.wxyz.inetdisk.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ren.wxyz.inetdisk.dao.UserDao;
import ren.wxyz.inetdisk.model.UserInfo;
import ren.wxyz.inetdisk.service.LoginService;
import ren.wxyz.inetdisk.util.Constants;
import ren.wxyz.inetdisk.util.DigitUtils;
import ren.wxyz.inetdisk.vo.LoginUser;


/**
 * <p>
 * 登录授权和注销
 * </p>
 * @Author wxyz
 * @CreateTime 2015-10-12 上午10:52:35 init class
 */
@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private UserDao userDao;
	
	@Override
	public LoginUser checkAccount(String account, String passwd) {
		LoginUser user = new LoginUser();
		
		// 获取访问的用户
		UserInfo userModel = userDao.findByAccount(account);
		
		// 验证账号的状态
		if (null == userModel || StringUtils.isBlank(passwd)) {
			// 用户名或密码错误
			user.setLoginStatus(Constants.MSG_LOGIN_ACCOUNT_ERROR);
		}
		else {
			// 锁定验证
			if (userModel.getLockFlag().equals("1")) {
				// 用户被锁定
				user.setLoginStatus(Constants.MSG_LOGIN_ACCOUNT_LOCKED);
			}
			else {
				String salt = userModel.getPasswdSalt();
				if (userModel.getPasswd().equals(DigitUtils.cryptPwd(passwd, salt))) {
					// 密码正确
					user.setLoginStatus(Constants.MSG_OK);
					
					// 设置登录信息
					user.setId(userModel.getId());
					user.setAccount(userModel.getAccount());
					user.setNickName(userModel.getNickName());
				}
				else {
					// 密码错误
					user.setLoginStatus(Constants.MSG_LOGIN_ACCOUNT_ERROR);
				}
				
			}
		}
		
		return user;
	}
}
